package com.game.state;

import javax.microedition.khronos.opengles.GL10;

import media.sound.GameSound;

import object.obj2D.graphics.TexSquare;
import object.obj2D.graphics.Texture;
import system.Device;
import system.GameOption;
import system.Graphics;
import system.KeyBoard;

import com.game.GamePlay;
import com.game.R;

public class About extends State {

	private final int MENU_ITEM_BACK = 0;
	private final int MENU_ITEM_BEGIN = MENU_ITEM_BACK;

	private int selected = MENU_ITEM_BEGIN;

	private GL10 gl;
	private int m_fSrcWidth;
	private int m_fSrcHeight;
	private int sNumMenuItem = 1;
	private int[] m_iMenuItemTex = new int[sNumMenuItem];
	private int[] m_iMenutemSelectedTex = new int[sNumMenuItem];
	private int m_itexBgID;

	private float m_fMenuItemHeight = Device.MENU_BUTTON_HEIGHT;
	private float m_fMenuItemWidth = Device.MENU_BUTTON_WIDTH;
	private Texture m_texture = new Texture();
	private TexSquare m_abtBg;
	private TexSquare m_abtItem;

	@Override
	public void Init() {
		gl = Graphics.GetGraphics();
		m_fSrcWidth = Device.GetWidth();
		m_fSrcHeight = Device.GetHeight();

		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glPushMatrix();
		gl.glLoadIdentity();
		float w = m_fSrcWidth / 2;
		float h = m_fSrcHeight / 2;
		gl.glOrthof(-w, w, -h, h, -1.0f, 1.0f);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPushMatrix();
		// Load menu item texture
		m_iMenuItemTex[MENU_ITEM_BACK] = m_texture
				.initTexture(R.drawable.mnu_back);
		m_iMenutemSelectedTex[MENU_ITEM_BACK] = m_texture
				.initTexture(R.drawable.mnu_back_select);
		m_abtItem = new TexSquare(Size.MNU_ITEM_WIDTH, Size.MNU_ITEM_HEIGHT);

		// Load background texture
		m_itexBgID = m_texture.initTexture(R.drawable.mnu_abt_bg);
		m_abtBg = new TexSquare(Size.SCREEN_WIDTH, Size.SCREEN_HEIGHT);
		
		LoadAbout();
	}

	@Override
	public void Update(GamePlay gamePlay) {
		UpdateAbout();
		super.Update(gamePlay);
	}

	@Override
	public void Render() {
		gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		
		gl.glColor4f(1.0f, 1.0f, 1.0f, m_aboutAlpha);
		
		// Back ground
		m_texture.loadTexture(m_itexBgID);
		m_abtBg.Render();

		DawAbout();
		
		// Draw menuItem

		for (int i = 0; i < sNumMenuItem; ++i) {
			gl.glPushMatrix();
			if (selected == i)
				m_texture.loadTexture(m_iMenutemSelectedTex[i]);
			else
				m_texture.loadTexture(m_iMenuItemTex[i]);
			gl.glTranslatef((2 * i - 1) * (m_fSrcWidth - m_fMenuItemWidth) / 2,
					-(m_fSrcHeight - m_fMenuItemHeight) / 2, 0);
			m_abtItem.Render();
			gl.glPopMatrix();
		}
		gl.glDisable(GL10.GL_TEXTURE_2D);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	}
	
	
	private int m_iAboutID;
	private TexSquare m_aboutSquare;
	private int m_iAboutWidth = 256;
	private int m_iAboutHeight = 256;
	
	private float m_aboutAlpha = 0;
	
	private boolean m_isUpdateAlpha = true;
	private long m_lastUpdateAlpha;
	private long UPDATE_ALPHA_TIME = 100;
	
	public void LoadAbout()
	{
		m_aboutSquare = new TexSquare(m_iAboutWidth, m_iAboutHeight);
		
		m_iAboutID = m_texture.initTexture(R.drawable.backcountry_about);
	}
	
	public void DawAbout()
	{	
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPushMatrix();
		
		m_texture.loadTexture(m_iAboutID);
		m_aboutSquare.Render();
		
		gl.glPopMatrix();
	}
	
	public void UpdateAbout()
	{
		if(m_isUpdateAlpha && (System.nanoTime() - m_lastUpdateAlpha)/1000000 >= UPDATE_ALPHA_TIME)
		{
			m_aboutAlpha += 0.1f;
			 m_lastUpdateAlpha = System.nanoTime();
			if(m_aboutAlpha >= 1.0f) m_isUpdateAlpha = false;
		}
		
	}

	@Override
	public void Destroy() {
		m_texture = null;
		m_abtBg = null;
		m_abtItem = null;
	}

	@Override
	public void ProcessKeyboard(GamePlay gamePlay) {
		// TODO Auto-generated method stub
		if (KeyBoard.IsKeyPressed(DK_PAD_MIDDLE)) {
			if(!gamePlay.ResumeState())
				gamePlay.SwitchState(GameState.MainMenu);
			if (GameOption.isSoundOn)
				GameSound.GetEffSound().PlaySelectMenu();
		}
	}

}
